package com.clss.commandhelper.function.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;

import com.clss.commandhelper.R;
import com.clss.commandhelper.base.BaseActivity;
import com.clss.commandhelper.function.home.HomeActivity;
import com.clss.commandhelper.tools.Constant;
import com.clss.commandhelper.tools.SpHelper;
import com.clss.commandhelper.tools.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * @author liuv
 */
public class LoginActivity extends BaseActivity implements LoginPresenter.LoginView,
        CompoundButton.OnCheckedChangeListener, EasyPermissions.PermissionCallbacks{
    private static final String TAG = "LoginActivity";

    @BindView(R.id.login_username_et)
    EditText loginNameEt;
    @BindView(R.id.login_password_et)
    EditText loginPasswordEt;
    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.login_username_status_iv)
    ImageView loginUsernameStatusIv;
    @BindView(R.id.login_password_status_cb)
    CheckBox loginPasswordStatusCb;

    private LoginPresenter mLoginPresenter;

    @Override
    protected int setViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        loginPasswordStatusCb.setOnCheckedChangeListener(this);
        loginPasswordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//        loginNameEt.setText("liuwei");
//        loginPasswordEt.setText("123456");
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        if (!TextUtils.isEmpty(SpHelper.getInstance().getToken())){
            Utils.startActivity(this, HomeActivity.class, null, true);
            return;
        }
        mLoginPresenter = new LoginPresenter();
        mLoginPresenter.setLoginView(this);

        EasyPermissions.requestPermissions(this,"请通过以下授权", 6, Constant.sPermissionsArray);
    }

    @Override
    public void requestFailure(Throwable t) {
        super.requestFailure(t);
    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);
    }

    @OnClick({R.id.login_btn, R.id.login_username_status_iv, R.id.logtin_forget_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                toLogin();
                break;
            case R.id.login_username_status_iv:
                loginNameEt.setText("");
                loginPasswordEt.setText("");
                break;
            case R.id.logtin_forget_tv:

                break;
            default:
        }
    }

    private void toLogin(){
        String username = loginNameEt.getText().toString().trim();
        if (TextUtils.isEmpty(username)){
            showShortToast(getString(R.string.please_input_username));
            return;
        }
        String password = loginPasswordEt.getText().toString().trim();
        if (TextUtils.isEmpty(password)){
            showShortToast(getString(R.string.please_input_password));
            return;
        }
        mLoginPresenter.toLogin(username, password);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Utils.setEditTextPasswordType(loginPasswordEt, isChecked);
    }

    @Override
    public void loginSuccess() {
        Utils.startActivity(this, HomeActivity.class, null, true);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
    }
}
